package org.jeie.actions;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;

import org.jeie.gui.panels.FillPanel;
import org.jeie.main.Action;
import org.jeie.messages.Messages;
import org.jeie.util.Util;

public class RectAction implements Action {
	public Point p1;
	public Point p2;
	public Color fore;
	public Color back;
	public FillPanel.Type type;
	public int size;

	public RectAction(
		Point p1,
		Point p2,
		Color fore,
		Color back,
		FillPanel.Type type,
		int size) {
		this.p1 = p1;
		this.p2 = p2;
		this.fore = fore;
		this.back = back;
		this.type = type;
		this.size = size;
	}

	public BufferedImage apply(BufferedImage img) {
		Graphics2D g = img.createGraphics();
		g.setStroke(new BasicStroke(size));
		boolean outline = false;
		boolean fill = false;
		switch (type) {
			case OUTLINE :
				outline = true;
				break;
			case FILL :
				fill = true;
				break;
			case FILL_OUTLINE :
				outline = true;
				fill = true;
				break;
		}

		if (fill) {
			g.setColor(type == FillPanel.Type.FILL ? fore : back);
			Util.fillGoodRect(g, p1.x, p1.y, p2.x, p2.y);
		}
		if (outline) {
			g.setColor(fore);
			Util.drawGoodRect(g, p1.x, p1.y, p2.x, p2.y);
		}
		g.dispose();

		return img;
	}

	public String getName() {
		return Messages.getString("Action.Rect"); //$NON-NLS-1$
	}
}
